var express = require('express');
var router = express.Router();
var { UserModel, StaffModel } = require("../utils/model")
var { aesEncrypt, keys, checkUsername } = require("../utils");
const { query } = require('express');

/* GET users listing. */
router.get('/', function (req, res, next) {
  res.send('respond with a resource');
});
router.get("/login", (req, res) => {
  res.render("login")
})

router.get("/register", (req, res) => {
  res.render("register")
})


// 注册
router.post("/register/submit", (req, res) => {
  var body = req.body;
  console.log(body)
  UserModel.findOne({
    username: body.username,
  }).then(data => {
    if (data) {
      // res.send("用户名已经被注册了...")
      res.send(`<script>alert('用户名已经被注册了...');location.href='/users/register'</script>`)
    } else {
      body.time = new Date();
      UserModel.insertMany(body)
        .then(result => {
          res.send(`<script>alert('恭喜你,注册成功');location.href='/users/login' </script>`)
        })
        .catch(err => {
          res.send(`<script>alert('注册失败');location.href='/users/register' </script>`)
        })
    }
  })
})

// 登录
// ajax 请求 接口  res.render  res.send   局部刷新  一般用 res.json   
router.post("/login/submit", (req, res) => {
  var body = req.body;
  // console.log(body)
  UserModel.findOne(body, { _id: 0 })
    .then(result => {
      if (result) {
        //  console.log(result)
        // 登录成功
        req.session.username = body.username;
        //  console.log(body.username)
        //  req.session.mobile =result.mobile;
        res.send(`<script>alert('登录成功...');location.href='/?username=${req.session.username}'</script>`)
        //  res.json({
        //      msg:"登录成功,即将进入首页",
        //      code:200,
        //      result,  // 用户信息 
        //      type:1 // 1 成功   
        //  })
        // res.send(`<script>alert('登录成功...')</script>`)
      } else {
        //  res.json({
        //    msg:"用户名或者密码不正确,请重新登录",
        //    code:200,
        //    type:0 // 0 失败 
        //  })
        res.send(`<script>alert('登录失败');location.href='/users/login' </script>`)
      }
    })
})

//去聊天室
router.get('/client', (req, res) => {

  checkUsername(req, res, username => {
    res.render('client', {
      username
    })
  })
})



//新增员工信息
router.post('/insert/submit', (req, res) => {
  var body = req.body;
  console.log(body)
  StaffModel.insertMany(body)
    .then(data => {
      res.send(`
 
    <script>alert('添加成功');location.href='/users/list' </script>
    `)

      // console.log(data)
    })


})


//查询员工列表
router.get('/list', (req, res) => {
  checkUsername(req, res, username => {
    StaffModel.find({}, {

    }).then(result => {
      // console.log(result)
      res.render('staff_list', { result, username })
    })
  })
})

// //删除
// router.get('/list/remove',(req,res)=>{
//   var {_id} = req.query;
//   console.log(_id)
//   StaffModel.remove({
//     _id
//   }).then(result=>{
//     console.log(result)
//     res.json({
//       code:200,
//       msg:'删除成功',
//       result
//     })
//      res.render('staff_list',{result})
//   })
// })

//查询员工列表
router.get('/list/set', (req, res) => {
  checkUsername(req, res, username => {
    StaffModel.find({}, {

    }).then(data => {

      // console.log(data)
      res.render('staff_set', { data, username })
    })
  })
})
//删除

router.get('/list/set/remove', (req, res) => {
  var { _id } = req.query;
  console.log(_id)
  checkUsername(req, res, username => {
    StaffModel.remove({
      _id
    }).then(data => {
      // console.log(data)
      res.json({
        code: 200,
        msg: '删除成功',
        data
      })
      res.render('staff_set', { data, username })
    })
  })
})

//修改
router.post('/list/set/update', (req, res) => {
  var { _id, name, tel, address, time, job, alt } = req.body
  console.log(_id, name, tel, address, time, job, alt)
  checkUsername(req, res, username => {
    StaffModel.updateMany({ _id }, {
      $set: { name, tel, address, time, job, alt }
    }).then(data => {
      // console.log(data)

      res.send(`
 
       <script>alert('修改成功');
       location.href='/users/list/set'
       </script>
       `)
      res.render('staff_set', { data, username })
    })
  })
})
router.get('/list/set/insert', (req, res) => {
  var { _id } = req.query
  checkUsername(req, res, username => {
  StaffModel.find({
    _id
  }).then(data => {
    res.json({
      code: '200',
      msg: '查询成功',
      data
    })
    res.render('staff_set', { data,username })
  })
})
})

module.exports = router;
